﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LW.DB;
using System.Data.SqlClient;
using System.Data;
using iLangl.LanguageWire.LWIntegration;
using iLangl.LanguageWire.DAL.iLanglDAL;
using iLangl.Model;

namespace iLangl.LanguageWire.DAL.LanguageWireDAL.RealStorage
{
	public class CompanyDAL : ICompanyDAL
	{
		/// <summary>
		/// Return LW Company Id or -1 is company not exists
		/// </summary>
		/// <param name="companyId">iLangl Company Id</param>
		/// <returns>LW Company Id or -1 if company not exists</returns>
		public Int32 GetLWCompanyId(Guid companyId)
		{
			DBProvider provider = new DBProvider();

			SqlParameter[] parameters = new SqlParameter[]
				{
					new SqlParameter("@CompanyGuid", companyId),
				};

			object result = provider.ExecuteScalar("Select CompanyId from Companies where Guid=@CompanyGuid", CommandType.Text, parameters);

			if ((result != null) && (result is int))
				return (int)result;

			return -1;
		}


		public void CreateCompany(Model.Company company)
		{
			int referenceNumber = GetLWCompanyId(new Guid(company.Id));
			company.TPIdentity = referenceNumber.ToString();
			if (referenceNumber > 0)
			{
				DALFacade.Instance.CompanyDAL.UpdateCompanyReference(company.Id, referenceNumber, null);

				return;
			}
			CreateCompanyRequest createCompanyRequest = new CreateCompanyRequest();

			createCompanyRequest.Guid = new Guid(company.Id);
			createCompanyRequest.AuthKey = LanguageWireConfiguration.Instance.AuthKey;
			createCompanyRequest.OriginId = LanguageWireConfiguration.Instance.OriginalCompanyId;
			createCompanyRequest.Name = company.Name;
			createCompanyRequest.PriceMatrixSourceCompanyId = LanguageWireConfiguration.Instance.PriceMatrixCompanyId; //?
			createCompanyRequest.ResponsibleAmId = LanguageWireConfiguration.Instance.ResponsibleAMId; //?
			createCompanyRequest.ResponsibleOfficeId = LanguageWireConfiguration.Instance.ResponsibleOfficeId;
			createCompanyRequest.ResponsiblePmId = LanguageWireConfiguration.Instance.ResponsiblePMId;
			if (createCompanyRequest.JobDefaultSettings == null)
				createCompanyRequest.JobDefaultSettings = new CompanyJobDefaultSettings();

			createCompanyRequest.JobDefaultSettings.EnforceAgitoTranslate = LanguageWireConfiguration.Instance.EnforceAgitoTranslate;
			createCompanyRequest.JobDefaultSettings.PermitAgitoTranslate = LanguageWireConfiguration.Instance.PermitAgitoTranslate;


			//Generate Internal Note
			StringBuilder internalNotes = new StringBuilder();

			internalNotes.AppendFormat("Country - {0}\r\n", company.Country);
			internalNotes.AppendFormat("State - {0}\r\n", company.State);
			internalNotes.AppendFormat("Address - {0}\r\n", company.Address);
			internalNotes.AppendFormat("Zip - {0}\r\n", company.Zip);
			internalNotes.AppendFormat("Phone - {0}\r\n", company.Phone);
			internalNotes.AppendFormat("Currency - {0}\r\n", company.Currency);

			createCompanyRequest.InternalNote = internalNotes.ToString();

			ClientManagementServiceClient client = new ClientManagementServiceClient();
			int companyId = client.CreateCompany(createCompanyRequest);

			company.TPIdentity = companyId.ToString();
			//Bind LW company Id to ilanglCompany in Local Store
			DALFacade.Instance.CompanyDAL.UpdateCompanyReference(company.Id, companyId, null);
		}

		public Company GetLWCompanyInfo()
		{
			Company result = new Company();

			result.Id = LWCompanyInfoConfiguration.Instance.CompanyId;
			result.Address = LWCompanyInfoConfiguration.Instance.CompanyAddress;
			result.City = LWCompanyInfoConfiguration.Instance.CompanyCity;
			result.Country = LWCompanyInfoConfiguration.Instance.CompanyCountry;
			result.Currency = LWCompanyInfoConfiguration.Instance.CompanyCurrency;
			result.Name = LWCompanyInfoConfiguration.Instance.CompanyName;
			result.Phone = LWCompanyInfoConfiguration.Instance.CompanyPhone;
			result.State = LWCompanyInfoConfiguration.Instance.CompanyState;
			result.Zip = LWCompanyInfoConfiguration.Instance.CompanyZip;

			result.BillingInfo = new BillingInfo();
			result.BillingInfo.Id = LWCompanyInfoConfiguration.Instance.BillingId;
			result.BillingInfo.BillingCity = LWCompanyInfoConfiguration.Instance.BillingCity;
			result.BillingInfo.BillingCountry = LWCompanyInfoConfiguration.Instance.BillingCountry;
			result.BillingInfo.BillingCurrency = LWCompanyInfoConfiguration.Instance.BillingCurrency;
			result.BillingInfo.BillingEmail = LWCompanyInfoConfiguration.Instance.BillingEmail;
			result.BillingInfo.BillingFirstName = LWCompanyInfoConfiguration.Instance.BillingFirstName;
			result.BillingInfo.BillingLastName = LWCompanyInfoConfiguration.Instance.BillingLastName;
			result.BillingInfo.BillingPostalCode = LWCompanyInfoConfiguration.Instance.BillingPostalCode;
			result.BillingInfo.BillingStreetAddress = LWCompanyInfoConfiguration.Instance.BillingStreetAddress;
			result.BillingInfo.BillingStreetAddress2 = LWCompanyInfoConfiguration.Instance.BillingStreetAddress2;
			result.BillingInfo.Company = result;

			result.BillingInfo.Name = result.Name;

			return result;
		}
	}
}
